package com.edu.springboot_disk_erban.controller;

import com.edu.springboot_disk_erban.dao.EchartsDao;
import com.edu.springboot_disk_erban.entity.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
public class EchartsController {

    @Resource
    EchartsDao echartsDao;

    @RequestMapping("/getUserCount")
    @ResponseBody
    public ResultModel<UserCounts> userCounts(){
        UserCounts userCounts = echartsDao.getUserCounts();

        ResultModel<UserCounts> resultModel = new ResultModel<UserCounts>();
        if (userCounts!=null){
            resultModel = ResultModel.success("查询成功", userCounts);
        }else {
            resultModel = ResultModel.error("查询失败");
        }
        return resultModel;
    }

    @RequestMapping("/getFileCount")
    @ResponseBody
    public ResultModel<FileCounts> fileCount(){
        FileCounts fileCounts = echartsDao.getFileCounts();

        ResultModel<FileCounts> resultModel = new ResultModel<>();
        if (fileCounts!=null){
            resultModel = ResultModel.success("查询成功", fileCounts);
        }else {
            resultModel = ResultModel.error("查询失败");
        }
        return resultModel;
    }

    @RequestMapping("/getFileCountByDay")
    @ResponseBody
    public ResultModel<FileByDayList> getFileCountByDay(){
        FileByDayList fileByDayList = new FileByDayList();
        ArrayList<String> name = new ArrayList<>();
        ArrayList<Long> value = new ArrayList<>();
        List<FileByDay> data = echartsDao.getFileByDayCounts();
        ResultModel<FileByDayList> resultModel = new ResultModel<>();
        System.out.println(data);
        for (FileByDay datum : data) {
            name.add(datum.getCreatetime());
            value.add(datum.getCounts());

        }
        fileByDayList.setName(name);
        fileByDayList.setValue(value);
        if (fileByDayList != null) {

            resultModel= ResultModel.success("查询成功", fileByDayList);

        }else {
            ResultModel.error("查询失败");
        }
        return resultModel;


    }


}
